<template>
  <div class="house-list">
    <el-table :data="tableData"
              border
              :fit="true"
              style="width: 100%">
      <el-table-column label="房屋id"
                       prop="houseid">
      </el-table-column>
      <el-table-column label="地址"
                       prop="address">
      </el-table-column>
      <el-table-column label="面积"
                       prop="area">
      </el-table-column>
      <el-table-column label="价格"
                       prop="price">
      </el-table-column>
      <el-table-column label="状态"
                       prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status==='已租赁'"
                  size="medium"
                  type="success">已租赁</el-tag>
          <el-tag v-else-if="scope.row.status === '已被申请'"
                  size="medium"
                  type="warning">已被申请</el-tag>
          <el-tag v-else
                  size="medium"
                  type="warning">未租赁</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" v-if="role==='admin'">
        <template slot-scope="scope">
          <el-button size="mini"
                     type="primary"
                     @click="handleEdit(scope.$index, scope.row)">修改</el-button>
          <el-button size="mini"
                     type="danger"
                     @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" v-if="role==='renter'">
        <template slot-scope="scope" >
          <el-button size="mini"
                     type="primary"
                     @click="applyViewHouse(scope.$index, scope.row)"
                     :disabled="scope.row.state==1?true:scope.row.state==2? true:false">{{scope.row | operattionVal(scope.row) }}</el-button>
        </template>
      </el-table-column>
    </el-table>
<!--    分頁-->
    <div class="pagination-box">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="pagination.currentPage"
        :page-size="pagination.pageSize"
        layout="total, prev, pager, next"
        :total="pagination.total">
      </el-pagination>
    </div>
    <el-dialog title="修改信息"
               :visible.sync="dialogVisible"
               width="40%"
               :before-close="handleClose">
      <HouseForm :showBtn="false" :houseItem="houseItem"></HouseForm>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="cancel()">取消</el-button>
        <el-button type="primary"
                   @click="sure()">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script lang="js">
 import HouseForm from '@/components/HouseForm.vue'
 import axios from 'axios'
 import { message } from 'ant-design-vue'
export default {
  name: 'Houselist',
  components: {
      HouseForm
    },
  data(){
    return {
      tableData: [],
        dialogVisible:false,
        role:'',
      houseItem:'',
      pagination: {//分页相关模型数据
        currentPage: 1,//当前页码
        pageSize: 10,//每页显示的记录数
        total: 0,//总记录数
        type:"",
        name:"",
        description:"",
      }
    }
  },
  filters:{
    operattionVal(row){
      console.log(row)
      if(row.status === "已租赁") {
        return '该房已被租赁'
      } else if(row.status === "已被申请"){
        return '该房已经被申请'
      } else {
        return '申请租赁'
      }
    }
  },
  beforeMount(){
    this.getRole()
  },
  created () {
    this.getAll()
  },
  methods: {
    getAll: function (){
      axios.get("/houselist/zuke/"+this.pagination.currentPage).then((res)=>{
        if (res.data.code === 1){
          this.tableData = res.data.data.records
          this.pagination.currentPage = res.data.data.current
          this.pagination.total = res.data.data.total
          this.pagination.pageSize = res.data.data.size
        }
      })
    },
      getRole(){
        this.role = this.$store.state.userInfo.role;
      },
      handleEdit(index, row) {
       console.log("表格",row);
        this.houseItem = row;
        this.dialogVisible = true;

      },

      handleDelete(index, row) {
          this.$confirm('确定要删除吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        }).then(() => {
          // 访问接口
          axios.delete("/houselist/"+row.id).then(res=>{
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getAll()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消成功'
          });
        });
      },
      handleClose(){
        this.dialogVisible = false;
      },
      cancel(){
        this.dialogVisible = false;
        this.$message.info("取消成功");
      },
      sure(){
        //访问修改接口
        axios.put("/houselist",this.houseItem).then((res)=>{
          if(res.data.code===1){
            this.houseItem =[];
            this.$message.success(res.data.data)
          }else if(res.data.code===0){
            this.$message.error("数据更新失败，将自动刷新页面")
          }
        }).finally(
        this.dialogVisible = false,
          this.getAll(),

        )

      },
      applyViewHouse(index,row){
      axios.get("/houselist/zuke/apply/"+row.houseid).then((res)=>{
        console.log(row.houseid)
        // 申请看房
        if (res.data.code ===1){
          this.$alert('申请成功，请耐心等待管理员联系您', '标题名称', {
            confirmButtonText: '确定',
            callback: action => {
              // 访问接口
              this.tableData[index].state = 2;
            }
          });
        }else {
          this.$message.error(res.data.msg)
        }
      }).finally(
        this.getAll
      )
      },
    //切换页码
    handleCurrentChange(currentPage) {
      this.pagination.currentPage = currentPage;
      this.getAll();
    },
    }

}
</script>
<style lang="scss" scoped>
</style>
